package org.brisling.sysman.queryService;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.sysman.domain.Department;
import org.brisling.sysman.domain.JobRole;
import org.brisling.sysman.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/departmentHis/query")
public class QueryDeptHisService extends AbstractBaseQueryService<Department>{
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	public QueryDeptHisService(){
		this.moduleName = ConstSysParam.MODULE_DEPARTMENT;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.VIEW_PREFIX +"sysman_department_his";
		this.clazz = Department.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("sortedid",null);			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}	
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<Department> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}	
	
	@RequestMapping("/getTreeListDomain.action")
	public ListDomain<Department> getTreeListDomain(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeListDomain(id);
		
	}	
	
	@RequestMapping("/getTreeList.action")
	public List<Department> getTreeList(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeList(id);
		
	}
	
	@RequestMapping("/getTreeListByOwner.action")
	public List<Department> getTreeListByOwner(@RequestParam(value="subsystem",required=false) String subsystem){
		String queryCode = null;
		if(subsystem!=null){
			queryCode = String.format("query_%1$s_all", subsystem);
		}
		
		
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Long deptid = null;
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, queryCode)){
			Users usr = SysConfig.getUsr(userid);
			if(usr!=null && usr.getDeptid()!=null){
				deptid = usr.getDeptid();
			}
		}
		
		if(deptid!=null)
			return this.getPagedCurrentTreeList(deptid);
		else
			return this.getPagedTreeList(deptid);
		
	}
	
	/**
	 * 根据用户岗位角色所在部门进行人员过滤
	 * @param subsystem
	 * @return
	 */
	@RequestMapping("/getTreeListByJobDept.action")
	public List<Department> getTreeListByJobDept(@RequestParam(value="subsystem",required=false) String subsystem){
		String queryCode = null;
		if(subsystem!=null){
			queryCode = String.format("query_%1$s_all", subsystem);
		}
		
		
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String deptid = null;
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, queryCode)){
			Users usr = SysConfig.getUsr(userid);
			if(usr!=null){
				List<JobRole> jobroles = usr.getJobrole();
				if(jobroles!=null && jobroles.size()>0){
					for(JobRole r:jobroles){
						if(r!=null && r.getDeptid()!=null)
						deptid = deptid ==null?r.getDeptid()+"":deptid+","+r.getDeptid();
					}
					 
				}else{
					if(usr.getDeptid()!=null){
						deptid = usr.getDeptid().toString();
					}
				}
			}
		}		
		
		return this.getTreeList(deptid);		
		
	}
	
	@RequestMapping("/getOne.action")
	public Department getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}

}
